package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.dto.NursingLevelPageQueryDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class NursingLevelServiceImpl implements NursingLevelService {
    final NursingLevelMapper nursingLevelMapper;


    /**
     * 分页查询护理等级信息
     *
     * @param dto
     */
    @Override
        public PageBean<NursingLevelVo> listByPage(NursingLevelPageQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Page<NursingLevelVo> page = nursingLevelMapper.listByPage(dto); ;
        return PageBean.of(page, NursingLevelVo.class);
    }

    /**
     * 新增护理等级信息
     *
     * @param dto
     */
    @Override
    public void insert(NursingLevelDto dto) {
        NursingLevel nursingLever = BeanUtil.toBean(dto, NursingLevel.class);
        nursingLever.setStatus(1);
        nursingLevelMapper.insert(nursingLever);
    }

    /**
     * 根据ID查询护理等级信息
     *
     * @param id
     */
    @Override
    public NursingLevelVo getById(Long id) {
        return nursingLevelMapper.getById(id);
    }

    /**
     * 更新护理等级信息
     *
     * @param dto
     */
    @Override
    public void update(NursingLevelDto dto) {
        NursingLevel nursingLever = BeanUtil.toBean(dto, NursingLevel.class);
        nursingLevelMapper.update(nursingLever);
    }

    /**
     * 删除护理等级信息
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        NursingLevelVo byId = nursingLevelMapper.getById(id);
        if (ObjectUtil.equal(byId.getStatus(), 1)){
            //启用状态不能删除
            throw new BaseException(BasicEnum.ENABLED_CANNOT_DELETED);
        }

        nursingLevelMapper.deleteById(id);
    }

    /**
     * 启用或禁用
     *
     * @param id
     * @param status
     */
    @Override
    public void enableOrDisable(Long id, Integer status) {
        NursingLevel nursingLevel = NursingLevel.builder().id(id).status(status).build();
        nursingLevelMapper.update(nursingLevel);
    }

    /**
     * 查询所有护理等级信息
     */
    @Override
    public List<NursingLevelVo> findAll() {
        return nursingLevelMapper.findAll();
    }




}


